/**
* 文件名：Util.java
* 创建日期：
* Copyright (c)  西安西安乐食
* All rights reserved.
*/
package com.uniknow.app.util;

import java.io.File;
import java.lang.reflect.Method;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;


/**
 * 
 * 描述:工具类
 * 
 * @version 1.00
 * @author zhangyao
 *
 */
public class Util {
	
	/**
	 * 
	 * 描述:网络类型
	 * 
	 * @version 1.00
	 * @author zy
	 *
	 */
	public static enum netType
	{
		WIFI, CMNET, CMWAP, NONE
	}

	/**
	 * 网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context)
	{
		ConnectivityManager mgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] info = mgr.getAllNetworkInfo();
		if (info != null)
		{
			for (int i = 0; i < info.length; i++)
			{
				if (info[i].getState() == NetworkInfo.State.CONNECTED)
				{
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断是否有网络连�?
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkConnected(Context context)
	{
		if (context != null)
		{
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null)
			{
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断WIFI网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWifiConnected(Context context)
	{
		if (context != null)
		{
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mWiFiNetworkInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mWiFiNetworkInfo != null)
			{
				return mWiFiNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断MOBILE网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isMobileConnected(Context context)
	{
		if (context != null)
		{
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mMobileNetworkInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (mMobileNetworkInfo != null)
			{
				return mMobileNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 获取当前网络连接的类型信方式
	 * 
	 * @param context
	 * @return
	 */
	public static int getConnectedType(Context context)
	{
		if (context != null)
		{
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null && mNetworkInfo.isAvailable())
			{
				return mNetworkInfo.getType();
			}
		}
		return -1;
	}

	/**	 
	 * 获取当前的网络状态-1：没有网1：WIFI网络2：wap 网络3：net网络
	 * 
	 * @param context
	 * 
	 * @return
	 */
	public static netType getAPNType(Context context)
	{
		ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo == null)
		{
			return netType.NONE;
		}
		int nType = networkInfo.getType();

		if (nType == ConnectivityManager.TYPE_MOBILE)
		{
			if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet"))
			{
				return netType.CMNET;
			}

			else
			{
				return netType.CMWAP;
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI)
		{
			return netType.WIFI;
		}
		return netType.NONE;

	}
	
	/**
	 * 获取应用版本 名称
	 * @param context
	 * @return
	 */
	public static String getPackageVesionName(Context context){
		String vesionName =  "";
		try {
			PackageInfo packinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			vesionName = packinfo.versionName;			
		} catch (Exception e) {
			// TODO: handle exception
		}
		return vesionName;
	}
	
	/**
	 *  获取版本 号
	 * @param context
	 * @return
	 */
	public static int getPackageVesionCode(Context context){
		int vesionCode = -1;
		try {
			// 0 表示版本信息
			PackageInfo packinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);			
			vesionCode = packinfo.versionCode;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return vesionCode;
	}
	
	
	public static boolean hasSmartBar() {
		
		try {
			// 新型号可用反射调用Build.hasSmartBar()
			Method method =	Class.forName("android.os.Build").getMethod("hasSmartBar");
			return ((Boolean) method.invoke(null)).booleanValue();
		} catch (Exception e) {
			
		}
		
		// 反射不到Build.hasSmartBar()，则用Build.DEVICE判断
		if (Build.DEVICE.equals("mx2")) {
			return true;
		} else if (Build.DEVICE.equals("mx") ||
						Build.DEVICE.equals("m9")) {
			return false;
		}
		
		return false;
	}
	
	/**
	 * 获取本应用的资源存储根目录
	 * @return
	 */
	public static String getDirectPath(Context context ){
		
		File cacheDir;
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)){
			cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"uniknow");
		}else{
			cacheDir = context.getCacheDir();
		}
		
		return cacheDir.getAbsolutePath() + File.separator;
	}	
	
}
